//@ sourceURL=/static/modules/details/details.js
let downloadFile = '/dmpTable/downloadTemplate?dataType=';
let tableUrl =  '/dmpTable/find';
let saveUrl =  '/dmpTable/saveData';
let viewUrl = '/dmpTable/selectTableColByTId?tId=';
let importUrl =  '/dmpTable/upload';
let tableLength
let flag = true
let tableList
var detailsView = new View({
    name: 'details',
    init_page() {
        $('#add_btn').click(function () {
            router.to('details_group')
            return false;
        });

    }
});
/**
 * 方法
 */
detailsView.fn = {
    render: {
        operation(data, type) {
            var html = '<button class="btn btn-primary margin-right-10 grid-delete-btn" data-name="'+data.dataType+'" data-id="' + data.id + '" data-tname="' + data.tName + '" data-hkey="' + data.hPKey + '" data-hvalue="' + data.hPValue + '" data-mapper="' + data.hNumMappers + '" data-hdfs="' + data.hDir + '" data-alias="' + data.tAlias + '" type="button" onClick="detailsView.fn.viewMessage(this)">查看</button>'
            return html;
        },
        numbermesage1(data,type,row){
            var html = '<input type="number" style="width:100px;" class="form-control" name="colLength_'+row.index+'" value="'+data+'">'
            return html
        },
        numbermesage2(data,type,row){
            var html = '<input type="number" style="width:100px;" class="form-control" name="hNumMappers_'+row.index+'" value="'+data+'">'
            return html
        },
        textmesage1(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="tName_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage2(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="tComments_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage3(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="tAlias_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage4(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="colName_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage5(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="colComments_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage6(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="colType_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage7(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="hPKey_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage8(data,type,row,b){
            var html = '<input type="text" style="width:100px;" class="form-control" name="hPValue_'+row.index+'"  value="'+data+'">'
            return html
        },
        textmesage9(data,type,row,b){
            var html = '<input type="text" class="form-control" name="hDir_'+row.index+'"  value="'+data+'">'
            return html
        },
       selectoperation1(data,type,row){
           if(data == "是"){
               var html = '<option value="是" selected>是</option><option value="否">否</option>'
           }else if(data == "否"){
               var html = '<option value="是">是</option><option value="否" selected>否</option>'
           }else {
               var html = '<option value="是">是</option><option value="否">否</option>'
           }

           return '<select class="selectlist form-control" name="nullable_'+row.index+'" data-plugin="selectpicker"> <option value="">请选择</option>'+html+'</select>'
        },
        selectoperation2(data,type,row){
            if(data == "是"){
                var html = '<option value="是" selected>是</option><option value="否">否</option>'
            }else if(data == "否"){
                var html = '<option value="是">是</option><option value="否" selected>否</option>'
            }else {
                var html = '<option value="是">是</option><option value="否">否</option>'
            }

            return '<select class="selectlist form-control" name="pkFlag_'+row.index+'" data-plugin="selectpicker"> <option value="">请选择</option>'+html+'</select>'
        }
    },
    //查看
    viewMessage: function (e) {
        var id = $(e).data("id")
        var type = $(e).data("name")
        $('#view_message_box').empty();
        if(type == 0){
            var html = '<p><span><i class="star"></i>表名：'+$(e).data("tname")+'</span><span class="view-title"><i class="star"></i>别名：'+$(e).data("alias")+'</span></p>'
            $('#view_message_box').append(html);
        }else if(type == 1){
            var html = '<p><span><i class="star"></i>表名：'+$(e).data("tname")+'</span><span class="view-title"><i class="star"></i>别名：'+$(e).data("alias")+'</span></p>' +
                '            <p><span><i class="star"></i>HIVE表分区列名：'+$(e).data("hkey")+'</span><span class="view-title"><i class="star"></i>HIVE表分区列值：'+$(e).data("hvalue")+'</span></p>' +
                '            <p><i class="star"></i>MAPPER数量：'+$(e).data("mapper")+'<span class="view-title"><i class="star"></i>HDFS目录：'+$(e).data("hdfs")+'</span></p>'
            $('#view_message_box').append(html);
        }
        wdAjax.post({
            url: viewUrl+id,
            cache: false, //禁用缓存
            success: function (data) {
                detailsView.plugins.member_file_dialog.show();
                detailsView.plugins.member_list_view.options.data = data;
            }
        })
       
    },
    //导入
    introgression:function(e){
        $('#upload_table_title').empty();
        $('#upload_table_content').empty();
        var ie = (navigator.appVersion.indexOf("MSIE")!=-1);
        var file = $("#myFile");
        if( ie ){
            var file2= file.cloneNode(false);
            file2.onchange= file.onchange;
            file.parentNode.replaceChild(file2,file);
        }else{
            $(file).val("");
        }
        $('#member_upfile_show').show();
        $('#member_upfile_hide').hide();
        detailsView.plugins.member_upfile_dialog.show();
    },
    //下载模板
    downfile:function(e){
        var url ='http://10.133.232.35:9999/dmpTable/downloadTemplate?dataType='+ e;
        window.location.href=url;
    },
    //导入
    importfile:function(e){
        var file = $("#myFile").val();
        if (file == "") {
            alertify.error("请选择要上传的文件");
            return false
        } else {
            //检验文件类型是否正确
            var exec = (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : '';
            if (exec != "xlsx") {
                alertify.error("文件格式不对，请上传后缀为'.xlsx'的Excel文件!");
                return false;
            }
        }
        // 通过FormData将文件转成二进制数据
        var formData = new FormData();
        // 将文件转二进制
        formData.append('file', e);
        // return
        $.ajax({
            url: 'http://10.133.232.35:9999/dmpTable/upload',
            type: 'POST',
            datatype: 'json',
            data: formData,
            cache:false,
            traditional: true,
            contentType: false,
            processData: false,
            success: function (data) {
                if(data.data){
                    if(data.data.list.length != 0){
                        alertify.success("上传成功");
                        data.data.list.forEach((item,index)=>{
                            item.index = index;
                        })
                        if(data.data.title.length == 9){
                            $('#upload_list_form2').hide();
                            $('#upload_list_form1').show();
                            flag = true
                            detailsView.plugins.upload_list_view1.options.data = data.data.list;
                            if(!detailsView.plugins.upload_list_view1._table){
                                detailsView.plugins.upload_list_view1.init();
                            }else{
                                detailsView.plugins.upload_list_view1._table.fnDraw()
                            }
                        }else if(data.data.title.length == 13){
                            $('#upload_list_form1').hide();
                            $('#upload_list_form2').show();
                            flag = false
                            detailsView.plugins.upload_list_view2.options.data = data.data.list;
                            if(!detailsView.plugins.upload_list_view2._table){
                                detailsView.plugins.upload_list_view2.init();
                            }else{
                                detailsView.plugins.upload_list_view2._table.fnDraw()
                            }
                        }
                        tableLength = data.data.list.length;
                        $('#member_upfile_show').hide();
                        $('#member_upfile_hide').show();
                    }else if(data.data.list.length == 0){
                        alertify.error("上传失败，请重新选择文件");
                    }
                }else{
                    alertify.error("上传失败，请重新选择文件");
                }


            },
            error: function (data) {
                alertify.error("上传失败，请重新选择文件");
            }
        });
    },
    //导入提交
    saveUpload:function(e){
        if(tableLength == 9){
            detailsView.plugins.upload_list_form1.init();
        }else if(tableLength == 13){
            detailsView.plugins.upload_list_form2.init();
        }

        if(flag){
            tableList = wdUtil.formToJson('#upload_list_form1')
        }else {
            tableList = wdUtil.formToJson('#upload_list_form2')
        }
        let tableData = []
        for(var i=0;i<tableLength;i++) {
            var obj = {};
            for(var key in tableList){
                if (key.split('_')[1] == i) {
                    obj[key.split('_')[0]] = tableList[key]
                }
            }
        tableData.push(obj)
        }
        console.log(tableData)
        tableData.forEach((item,index)=>{
            if(tableLength == 9){
                if(item.tName == ''||item.tAlias == ''||item.colName == ''||item.nullable == ''||item.pkFlag == ''||item.colType == ''||item.colLength == ''){
                    alertify.error("除表注释外其他内容不能为空");
                    return
                }
            }else if(tableLength == 13){
                if(item.tName == ''||item.tAlias == ''||item.colName == ''||item.nullable == ''||item.pkFlag == ''||item.colType == ''||item.colLength == ''||item.hPKey == ''||item.hPValue == ''||item.hNumMappers == ''||item.hDir == ''){
                    alertify.error("除表注释外其他内容不能为空");
                    return
                }
            }
        })
        // return
        wdAjax.post({
            url: saveUrl,
            cache: false, //禁用缓存
            data:tableData,
            success: result => {
                alertify.success("操作成功");
                detailsView.plugins.member_upfile_dialog.close();
                detailsView.plugins.details_list.refresh();
            }
        })


    },
}
/**
 * 事件
 */
detailsView._event = () => {

}

/**
 * 绑定插件
 */
//表格显示搜索
detailsView.use(new Table({
    name:'details_list',
    selector: '#details_list',
    url: tableUrl,
    search_btn: "#search_btn",
    callback: function () {

    }
}))
//查看表格
detailsView.use(new Table({
    name: 'member_list_view',
    selector: '#member_list_view',
    paging: false,//设置无分页
    search_btn:false,
    data:[],
    delay: true,//默认会执行init方法,如果添加次参数就不会执行init方法,需要手动调用
    init_complete() {

    }
}))
//导入表格
detailsView.use(new Table({
    name: 'upload_list_view1',
    selector: '#upload_list_view1',
    paging: false,//设置无分页
    search_btn:false,
    data:[],
    delay: true,//默认会执行init方法,如果添加次参数就不会执行init方法,需要手动调用
    init_complete() {

    }
}))
detailsView.use(new Table({
    name: 'upload_list_view2',
    selector: '#upload_list_view2',
    search_btn:false,
    paging: false,//设置无分页
    data:[],
    delay: true,//默认会执行init方法,如果添加次参数就不会执行init方法,需要手动调用
    init_complete() {

    }
}))
//查看弹窗
detailsView.use(new Dialog({
    name: 'member_file_dialog',
    title: '查看',
    width:'926px',
    container_selector: '#member_file_container',
    init_callback: function () {
        //执行表格的初始化 
        
        if(!detailsView.plugins.member_list_view._table){
            detailsView.plugins.member_list_view.init();
        }else{
            detailsView.plugins.member_list_view._table.fnDraw()
        }
    }
}))
//导入弹窗
detailsView.use(new Dialog({
    name: 'member_upfile_dialog',
    width:'1300px',
    title: '导入表结构',
    container_selector: '#member_upfile_container',
    init_callback: function () {
        // employeeManageView.use(new Form)
        // detailsView.plugins['upload_list_view'].init();
    }
}))

//导入表格提交表单
detailsView.use(new Form({
    name: 'upload_list_form1',
    selector: '#upload_list_form1',
    delay: true,
    init_callback() {}

}))
detailsView.use(new Form({
    name: 'upload_list_form2',
    selector: '#upload_list_form2',
    delay: true,
    init_callback() {}

}))